package com.lxk.service.impl;

import com.lxk.service.UtilService;
import com.lxk.util.OSSClientUtil;
import com.lxk.util.exception.GlobalException;
import com.lxk.vo.result.CodeMsg;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author JohnSoybean
 * @create 2020/3/26 11:08
 * @description
 */
@Service
@Transactional
public class UtilServiceImpl implements UtilService {

    public String uploadPicture(MultipartFile file) {
        OSSClientUtil oss = new OSSClientUtil();
        String fileName = oss.uploadImg2Oss(file);
        String url = oss.getUrl(fileName);
        oss.destory();
        return url;
    }

    public Map<String, Double> substringLnglat(String lnglat) {
        Map<String, Double> map = new HashMap<>();
        String[] strs = lnglat.split(",");
        if(strs.length==2){
            double lng = Double.parseDouble(strs[0]);
            double lat = Double.parseDouble(strs[1]);
            map.put("lng", lng);
            map.put("lat", lat);
            return map;
        }else{
            throw new GlobalException(CodeMsg.INVALID_LNGLAT);
        }
    }

}
